package com.hgnu.dorm.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hgnu.dorm.domain.dto.BaseDto;
import com.hgnu.dorm.domain.po.Student;
import com.hgnu.dorm.domain.vo.StudentVo;
import com.hgnu.dorm.domain.po.User;
import com.hgnu.dorm.domain.vo.StudentVo;
import com.hgnu.dorm.exception.BaseException;
import com.hgnu.dorm.service.StudentService;
import com.hgnu.dorm.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping()
@Slf4j
@Tag(name = "系统基本接口")
public class BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;

    @PostMapping("/login")
    @Operation(summary = "用户登录接口")
    public SaResult login(@RequestBody BaseDto dto){
        String identity = dto.getIdentity();
        if (StringUtils.isBlank(identity)){
            throw new BaseException("请选择登录身份");
        }
        if(identity.equals("admin")){
            User user = new User();
            user.setAccount(dto.getAccount());
            user.setPassword(dto.getPassword());
            User u = userService.login(user);
            if(u != null){
                String id = u.getId().toString()+","+identity;
                StpUtil.login(id);
                List<String> permissionList = StpUtil.getPermissionList();
                Map<String,Object> map = new HashMap<>();
                map.put("token",StpUtil.getTokenValue());
                map.put("permission",permissionList);
                return SaResult.data(map);
            }
        }else {
            Student student = new Student();
            student.setStudentId(dto.getAccount());
            student.setPassword(dto.getPassword());
            Student s = studentService.login(student);
            if(s != null){
                String id = s.getId().toString()+","+identity;
                StpUtil.login(id);
                List<String> permissionList = StpUtil.getPermissionList();
                Map<String,Object> map = new HashMap<>();
                map.put("token",StpUtil.getTokenValue());
                map.put("permission",permissionList);
                return SaResult.data(map);
            }
        }

        return SaResult.error("账号或密码错误");
    }
//    @PostMapping("/register")
//    @Operation(summary = "用户注册接口")
//    public SaResult register(@RequestBody User user){
//        log.info("账号：{}",user.getAccount());
//        userService.register(user);
//        return SaResult.ok();
//    }

    /*用户退出登录*/
    @PostMapping("/logout")
    @Operation(summary = "用户退出接口")
    public SaResult logout(){
        log.info("撤销token:{}",StpUtil.getTokenValue());
        StpUtil.logout();
        return SaResult.ok();
    }

    /**
     * 查询个人信息
     */
    @GetMapping("/info")
    @Operation(summary = "查询个人信息")
    public SaResult info(){
        Object loginId = StpUtil.getLoginId();
        String[] split = loginId.toString().split(",");
        String id = split[0];
        String currentIdentity = split[1];
        if(currentIdentity.equals("admin")){
            Map resp = new HashMap();
            User user = userService.getById(Integer.parseInt(id));
            resp.put("identity","admin");
            resp.put("user",user);
            return SaResult.data(resp);
        }else {
            StudentVo studentVo = studentService.selectStudentById(Integer.parseInt(id));
            Map resp = new HashMap();
            resp.put("identity","student");
            resp.put("user",studentVo);
            return SaResult.data(resp);
        }
    }
}
